<?php
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
/*
	[天生创想] (C)2008-2011 515158 Inc.
	This is NOT a freeware, use is subject to license terms
    URL:http://www.515158.com/
*/

get_config_keytable("config_usergroup");
empty($do) && $do = 'list';
$message = array(
	1 => '成功添加一个用户组。',
	2 => '成功编辑一个用户组。',
	3 => '成功删除选中的用户组。',
	4 => '请选择要删除的用户组。',
);

if ($do == 'list') {

	$wheresql = '';
	if ( $keyword = getGP('keyword','G') ) {
		$wheresql .= " AND groupname LIKE '%$keyword%'";
		$url .= '&keyword='.rawurlencode($keyword);
	}
	$sql = "SELECT * FROM ".DB_TABLEPRE."usergroup WHERE 1 $wheresql";
	$query = $db->query($sql);
	$result = array();
	while ($row = $db->fetch_array($query)) {
		$row['usercount'] = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."user WHERE groupid = '$row[id]'");
		$result[] = $row;
	}
	include_once('template/usergroup.php');

} elseif ($do == 'add') {
    get_config_keytable("config_usergroup_Increase");
	include_once('template/usergroup_add.php');

} elseif ($do == 'edit') {
    
	get_config_keytable("config_usergroup_edit");
	$id = getGP('id','G','int');
	$usergroup = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."usergroup WHERE id = '$id'");
	$purview = unserialize($usergroup['purview']);

	function checked($pv, $n) {
		global $purview;
		if (array_key_exists($pv, $purview) && $purview[$pv]) {
			echo $n == 1 ? 'checked="checked"' : '';
		} else {
			echo $n == 0 ? 'checked="checked"' : '';
		}
	}

	include_once('template/usergroup_edit.php');

} elseif ($do == 'save') {

	$id = getGP('id','P','int');
	$savetype = getGP('savetype','P');
	$name = getGP('groupname','P');
	$purview = getGP('purview','P','array');
	$usergroup = array(
		'groupname' => $name,
		'purview' => serialize($purview)
	);
	if ($savetype == 'new') {
		insert_db('usergroup',$usergroup);
		//recache('usergroup');
		usergroup_recache();
		$gid=$db->insert_id();
	if($gid!='')
	{
   $oalog = array(
		'uid' => $_USER->id,
		'content' => serialize($purview),
		'title' => '添加新用户组',
		'startdate' => get_date('Y-m-d H:i:s',PHP_TIME),
		'contentid' => $gid,
		'type' => '2'
	);
	insert_db('oalog',$oalog);
	
	}
		goto_page('admin.php?ac=usergroup&message=1&fileurl=user');
	} elseif ($savetype == 'edit') {
		update_db('usergroup',$usergroup, array('id' => $id));
		//recache('usergroup');
		usergroup_recache();
	if($id!='')
	{
   $oalog = array(
		'uid' => $_USER->id,
		'content' => serialize($purview),
		'title' => '修改用户组',
		'startdate' => get_date('Y-m-d H:i:s',PHP_TIME),
		'contentid' => $id,
		'type' => '2'
	);
	insert_db('oalog',$oalog);
	
	}
		goto_page('admin.php?ac=usergroup&message=2&fileurl=user');
	}

} elseif ($do == '删 除') {
	
	get_config_keytable("config_usergroup_delete");
	$idarr = getGP('id','P','array');
	if (count($idarr)) {
		foreach ($idarr as $id) {
			$db->query("DELETE FROM ".DB_TABLEPRE."usergroup WHERE `type` = 'user' AND id = '$id'");
			//$db->query("UPDATE ".DB_TABLEPRE."user SET groupid = 2 WHERE groupid = '$id'");
		}
	} else {
		prompt($message[4]);
	}
	//recache('usergroup');
	usergroup_recache();
	if($id!='')
	{
   $oalog = array(
		'uid' => $_USER->id,
		'content' => serialize($purview),
		'title' => '删除用户组',
		'startdate' => get_date('Y-m-d H:i:s',PHP_TIME),
		'contentid' => $id,
		'type' => '2'
	);
	insert_db('oalog',$oalog);
	}
	goto_page('admin.php?ac=usergroup&message=3&fileurl=user');

}

function usergroup_recache() {
	global $db;
	$sql = "SELECT * FROM ".DB_TABLEPRE."usergroup";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$content = "<?php\n//该文件是系统自动生成的缓存文件，请勿修改\n//创建时间：".get_date('Y-m-d H:i:s',time())."\n\nif (!defined('IN_TOA')) {exit('Access Denied!');}\n\n";
		$content .= "\$groupname = '$row[groupname]';\n";
		$content .= "\$purview = ".var_export(unserialize($row['purview']), true);
		$content .= "\n?>";
		$filename = CACHE_ROOT.'cache_usergroup_'.$row['id'].'.php';
		file_put_contents($filename, $content);
		@chmod($filename, 0777);
		
	}
	
}
//定义3
function get_keytable_view($fatherid=0,$seleid=0)
{
    global $db;
	$query = $db->query("SELECT * FROM ".DB_TABLEPRE."keytable where fatherid='$fatherid'  ORDER BY number Asc  ");
	
	while ($row = $db->fetch_array($query)) {
	if($row["type"]=='1'){
	echo '<input type="radio" name="purview['.$row["inputname"].']" value="'.$row["inputvalue"].'" class="radio" ';
	if($seleid!='0'){
	echo checked(''.$row["inputname"].'',$row["inputvalue"]);
	}else{
	if($row["inputchecked"]=='1'){
	echo ' checked="checked" ';
	}	
	}	
	echo '/>'.$row["name"].'';
	}else{
    echo '<input type="checkbox" name="purview['.$row["inputname"].']" value="'.$row["inputvalue"].'"';
	if($seleid!=0){
	echo checked(''.$row["inputname"].'',$row["inputvalue"]);
	}else{
	if($row["inputchecked"]=='1'){
	echo 'checked="checked" ';
	}	
	}	
	echo '>'.$row["name"].'';
	}

	}
	
   return ;

}
//////////////////
function get_keytable_list($fatherid=0,$seleid=0)
{
    global $db;
	$query = $db->query("SELECT * FROM ".DB_TABLEPRE."keytable where fatherid='$fatherid'  ORDER BY number Asc  ");
	
	while ($row = $db->fetch_array($query)) {
	echo '<tr>';
    echo ' <td nowrap class="TableData"> '.$row["name"].'：</td>';
    echo '  <td class="TableData">';
	get_keytable_view($row["id"],$seleid);
    //echo '  <input type="radio" name="purview[config_inc]" value="1" class="radio" />允许';
	//echo '<input type="radio" name="purview[config_inc]" value="0" class="radio" checked="checked" />禁止';
	echo '</td> </tr>';

	}
	
   return ;

}